import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Dashboard from '@/components/Dashboard'

import GameList from '@/components/game/list'
// import BookCategoryList from '@/components/bookcategory/list'
import TypeList from '@/components/type/list'
import CommentList from '@/components/comment/list'
import CollectionList from '@/components/collection/list'
import ShopCartList from '@/components/shopcart/list'

import UserList from '@/components/user/list'

// 懒加载方式，当路由被访问的时候才加载对应组件
const Login = resolve => require(['@/components/Login'], resolve)

Vue.use(Router)

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: '登录',
      component: Login
    },
    {
      path: '/',
      name: 'home',
      component: Home,
      redirect: '/dashboard',
      leaf: true, // 只有一个节点
      menuShow: true,
      iconCls: 'iconfont icon-1', // 图标样式class
      children: [
        {path: '/dashboard', component: Dashboard, name: '首页', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '用户管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-yonghu', // 图标样式class
      children: [
        {path: '/user/list', component: UserList, name: '用户列表', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '游戏管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-youxi',
      children: [
        {path: '/game/list', component: GameList, name: '游戏列表', menuShow: true},
        // {path: '/game/details', component: BookCategoryList, name: '图书详情', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '类型管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-17',
      children: [
        {path: '/type/list', component: TypeList, name: '类型列表', menuShow: true},
        // {path: '/book/category', component: BookCategoryList, name: '图书分类', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '评论管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-pinglun',
      children: [
        {path: '/comment/list', component: CommentList, name: '评论列表', menuShow: true},
        // {path: '/book/category', component: BookCategoryList, name: '图书分类', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '收藏管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-shoucang',
      children: [
        {path: '/collection/list', component: CollectionList, name: '收藏列表', menuShow: true},
        // {path: '/book/category', component: BookCategoryList, name: '图书分类', menuShow: true}
      ]
    },

    {
      path: '/',
      component: Home,
      name: '购物车管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-gouwuche',
      children: [
        {path: '/shopcar/list', component: ShopCartList, name: '购物车列表', menuShow: true},
        // {path: '/book/category', component: BookCategoryList, name: '图书分类', menuShow: true}
      ]
    },
    // {
    //   path: '/',
    //   component: Home,
    //   name: '设置',
    //   menuShow: true,
    //   iconCls: 'iconfont icon-setting1',
    //   children: [
    //     {path: '/user/profile', component: UserProfile, name: '个人信息', menuShow: true},
    //     {path: '/user/changepwd', component: UserChangePwd, name: '修改密码', menuShow: true}
    //   ]
    // }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log('to:' + to.path)
  if (to.path.startsWith('/login')) {
    window.localStorage.removeItem('access-user')
    next()
  } else {
    let user = JSON.parse(window.localStorage.getItem('access-user'))
    if (!user) {
      next({path: '/login'})
    } else {
      next()
    }
  }
})

export default router
